using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Common;
using Common.Enum;
using Converters;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing.Pictures;
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using SExperiment;
using SExperiment.ExperimentResult;
using SProject.Paramter;
using SSettings;

namespace Report
{
	public class PatientReportCreator
	{
		public delegate string GetSamplePicture(List<Sample> Samples, double width, double height, List<int> TubeID, List<int> ChannelNo);

		public const string KeyStart = "$";

		public const string KeyEnd = "#";

		public const char SpliteWord = '|';

		public static string KeyExperimentName = "$TestName";

		public static string KeyHospital = Utility.GetMessage("experimnet_result_hospital");

		public static string KeyDate = "$Date";

		public static string KeyStartTime = "$StartTime";

		public static string KeyEndTime = "$EndTime";

		public static string KeyReagentVer = "$ReagentVer";

		public static string KeyInsNum = "$InsNum";

		public static string KeyLabel = Utility.GetMessage("experimnet_result_label");

		public static string KeyName = Utility.GetMessage("experimnet_result_name");

		public static string KeySex = Utility.GetMessage("experimnet_result_sex");

		public static string KeyAge = Utility.GetMessage("experimnet_result_age");

		public static string KeyCaseID = Utility.GetMessage("experimnet_result_caseid");

		public static string KeyBedID = Utility.GetMessage("experimnet_result_bedid");

		public static string KeyOutPatientID = Utility.GetMessage("experimnet_result_outpatientid");

		public static string KeyInPatientID = Utility.GetMessage("experimnet_result_inpatientid");

		public static string KeyDetecdtDate = Utility.GetMessage("experimnet_result_detectdate");

		public static string KeyTestDate = Utility.GetMessage("experimnet_result_testdate");

		public static string KeyReportDate = Utility.GetMessage("experimnet_result_reportdate");

		public static string KeyOffice = Utility.GetMessage("experimnet_result_office");

		public static string KeyDiagnosis = Utility.GetMessage("experimnet_result_diagnosis");

		public static string KeySampleType = Utility.GetMessage("experimnet_result_sampletype");

		public static string KeyDoctor = Utility.GetMessage("experimnet_result_doctor");

		public static string KeyOperator = Utility.GetMessage("experimnet_result_operator");

		public static string KeyAssessor = Utility.GetMessage("experimnet_result_assessor");

		public static string KeyProject = Utility.GetMessage("experimnet_result_project");

		public static string KeyUnit = Utility.GetMessage("experimnet_result_unit");

		public static string KeyTargetName = Utility.GetMessage("experimnet_result_targetname");

		public static string KeyTargetResult = Utility.GetMessage("experimnet_result_targetresult");

		public static string KeyThreshold = Utility.GetMessage("experimnet_result_threshold");

		public static string KeyTargetConclusion = Utility.GetMessage("experimnet_result_targetconclusion");

		public static string KeyWellConclusion = Utility.GetMessage("experimnet_result_wellconclusion");

		public static string KeySampleConclusion = Utility.GetMessage("experimnet_result_sampleconclusion");

		public static string KeyRemark = Utility.GetMessage("experimnet_result_remark");

		public static string KeyCt = Utility.GetMessage("experimnet_result_ct");

		public static string KeyCn = Utility.GetMessage("experimnet_result_cn");

		public static string KeyADTyping = "$adtype";

		public static string KeyADQualityFactor = "$adqualityfactor";

		public static string KeyHRMTyping = "$hrmtype";

		public static string KeyHRMQualityFactor = "$hrmqualityfactor";

		public static string KeyCurve = Utility.GetMessage("experimnet_result_curve");

		public Experiment Experiment { get; set; }

		public List<Sample> Samples { get; set; }

		public string WrodFile { get; set; }

		public int ExportSamplesResultToWord(GetSamplePicture getSamplePicture)
		{
			if (string.IsNullOrEmpty(WrodFile) || !File.Exists(WrodFile))
			{
				return -1;
			}
			WordprocessingDocument wordprocessingDocument = null;
			try
			{
				wordprocessingDocument = WordprocessingDocument.Open(WrodFile, true);
				Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
				List<DocumentFormat.OpenXml.Wordprocessing.Text> textList = new List<DocumentFormat.OpenXml.Wordprocessing.Text>();
				List<Drawing> drawingList = new List<Drawing>();
				WordOperator.EnumerateTextAndDrawing(body.ChildElements, textList, drawingList);
				foreach (HeaderPart headerPart in wordprocessingDocument.MainDocumentPart.HeaderParts)
				{
					WordOperator.EnumerateTextAndDrawing(headerPart.Header.ChildElements, textList, drawingList);
				}
				foreach (FooterPart footerPart in wordprocessingDocument.MainDocumentPart.FooterParts)
				{
					WordOperator.EnumerateTextAndDrawing(footerPart.Footer.ChildElements, textList, drawingList);
				}
				UpdateBasicInfo(textList);
				UpdataDrawing(wordprocessingDocument.MainDocumentPart, drawingList, getSamplePicture);
				UpdateCustom(wordprocessingDocument);
				wordprocessingDocument.MainDocumentPart.Document.Save();
			}
			catch (Exception ex)
			{
				Log.WriteLine(ex.StackTrace);
			}
			finally
			{
				if (wordprocessingDocument != null)
				{
					wordprocessingDocument.Close();
					wordprocessingDocument = null;
				}
			}
			return 0;
		}

		protected virtual void UpdateCustom(WordprocessingDocument docx)
		{
		}

		private void UpdataDrawing(MainDocumentPart mainPart, List<Drawing> drawingList, GetSamplePicture getSamplePicture)
		{
			List<ImageInfo> list = new List<ImageInfo>();
			foreach (Drawing drawing2 in drawingList)
			{
				Drawing drawing = drawing2;
				if (drawing.Inline != null)
				{
					if (drawing.Inline.DocProperties != null && (!string.IsNullOrEmpty(drawing.Inline.DocProperties.Title) || !string.IsNullOrEmpty(drawing.Inline.DocProperties.Description)))
					{
						list.Add(new ImageInfo
						{
							Title = drawing.Inline.DocProperties.Title,
							Description = drawing.Inline.DocProperties.Description,
							Graphic = drawing.Inline.Graphic
						});
					}
				}
				else
				{
					if (drawing.Anchor == null)
					{
						continue;
					}
					OpenXmlElement openXmlElement = drawing.Anchor.ChildElements.Where((OpenXmlElement s) => s is DocProperties).FirstOrDefault();
					if (openXmlElement != null)
					{
						DocProperties docProperties = openXmlElement as DocProperties;
						openXmlElement = drawing.Anchor.ChildElements.Where((OpenXmlElement s) => s is Graphic).FirstOrDefault();
						if (openXmlElement != null && (!string.IsNullOrEmpty(docProperties.Title) || !string.IsNullOrEmpty(docProperties.Description)))
						{
							list.Add(new ImageInfo
							{
								Title = docProperties.Title,
								Description = docProperties.Description,
								Graphic = (openXmlElement as Graphic)
							});
						}
					}
				}
			}
			foreach (ImageInfo item in list)
			{
				string text = null;
				if (!string.IsNullOrEmpty(item.Title) && item.Title.StartsWith("$"))
				{
					text = item.Title;
				}
				else if (!string.IsNullOrEmpty(item.Description) && item.Description.StartsWith("$"))
				{
					text = item.Description;
				}
				if (string.IsNullOrEmpty(text))
				{
					continue;
				}
				if (text.IndexOf(KeyCurve) == 0 && getSamplePicture != null)
				{
					List<int> list2 = new List<int>();
					List<int> list3 = new List<int>();
					string[] array = text.Split(new string[1] { "\n" }, StringSplitOptions.None);
					foreach (string text2 in array)
					{
						if (!string.IsNullOrEmpty(text2))
						{
							WordKey key = GetKey(text2);
							list2.Add(key.TubeNo);
							list3.Add(key.ChannelNo);
						}
					}
					foreach (OpenXmlElement childElement in item.Graphic.GraphicData.ChildElements)
					{
						if (!(childElement is DocumentFormat.OpenXml.Drawing.Pictures.Picture))
						{
							continue;
						}
						IList<DocumentFormat.OpenXml.Drawing.Pictures.BlipFill> list4 = childElement.ChildElements.Where((OpenXmlElement s) => s is DocumentFormat.OpenXml.Drawing.Pictures.BlipFill).Cast<DocumentFormat.OpenXml.Drawing.Pictures.BlipFill>().ToList();
						if (list4.Count <= 0)
						{
							continue;
						}
						ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
						FileStream fileStream = null;
						try
						{
							DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties shapeProperties = childElement.ChildElements.Where((OpenXmlElement s) => s is DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties).Cast<DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties>().FirstOrDefault();
							if (shapeProperties == null || shapeProperties.Transform2D == null)
							{
								break;
							}
							Extents extents = shapeProperties.Transform2D.Extents;
							if (extents != null)
							{
								double num = 37.823598615971484;
								double width = (double)(long)extents.Cx / 360000.0 * num;
								double height = (double)(long)extents.Cy / 360000.0 * num;
								string text3 = getSamplePicture(Samples, width, height, list2, list3);
								if (!string.IsNullOrEmpty(text3))
								{
									fileStream = new FileStream(text3, FileMode.Open);
									imagePart.FeedData(fileStream);
									list4[0].Blip.Embed = mainPart.GetIdOfPart(imagePart);
								}
							}
						}
						catch (Exception)
						{
						}
						finally
						{
							if (fileStream != null)
							{
								fileStream.Close();
							}
						}
						break;
					}
					continue;
				}
				WordKey key2 = GetKey(text);
				if (key2 == null)
				{
					continue;
				}
				string value = GetKeyValue(key2);
				if (string.IsNullOrEmpty(value))
				{
					continue;
				}
				ImageInfo imageInfo = list.Where((ImageInfo s) => s.Title == value || s.Description == value).FirstOrDefault();
				if (imageInfo == null)
				{
					continue;
				}
				foreach (OpenXmlElement childElement2 in item.Graphic.GraphicData.ChildElements)
				{
					if (childElement2 is DocumentFormat.OpenXml.Drawing.Pictures.Picture)
					{
						IList<DocumentFormat.OpenXml.Drawing.Pictures.BlipFill> list5 = childElement2.ChildElements.Where((OpenXmlElement s) => s is DocumentFormat.OpenXml.Drawing.Pictures.BlipFill).Cast<DocumentFormat.OpenXml.Drawing.Pictures.BlipFill>().ToList();
						if (list5.Count > 0)
						{
							list5[0].Blip.Embed = GetGraplicBlip(imageInfo.Graphic);
							break;
						}
					}
				}
			}
		}

		private StringValue GetGraplicBlip(Graphic graplic)
		{
			StringValue result = null;
			foreach (OpenXmlElement childElement in graplic.GraphicData.ChildElements)
			{
				if (childElement is DocumentFormat.OpenXml.Drawing.Pictures.Picture)
				{
					DocumentFormat.OpenXml.Drawing.Pictures.BlipFill blipFill = childElement.ChildElements.Where((OpenXmlElement s) => s is DocumentFormat.OpenXml.Drawing.Pictures.BlipFill).Cast<DocumentFormat.OpenXml.Drawing.Pictures.BlipFill>().FirstOrDefault();
					if (blipFill != null)
					{
						result = blipFill.Blip.Embed;
						break;
					}
				}
			}
			return result;
		}

		private void UpdateBasicInfo(List<DocumentFormat.OpenXml.Wordprocessing.Text> textList)
		{
			if (textList == null || textList.Count == 0)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			foreach (DocumentFormat.OpenXml.Wordprocessing.Text text3 in textList)
			{
				stringBuilder.Append(text3.Text);
			}
			string text = stringBuilder.ToString();
			int startIndex = 0;
			int num = 0;
			int num2 = 0;
			List<WordKey> list = new List<WordKey>();
			while ((startIndex = text.IndexOf("$", startIndex)) != -1)
			{
				num2 = text.IndexOf("#", startIndex);
				num = text.IndexOf("$", startIndex + 1);
				if (num != -1 && num < num2)
				{
					startIndex++;
					continue;
				}
				WordKey key = GetKey(text.Substring(startIndex, num2 - startIndex + 1));
				if (key != null)
				{
					list.Add(key);
					string text2 = "";
					int num3 = 0;
					for (int i = 0; i < textList.Count; i++)
					{
						if (text2 == key.Content)
						{
							break;
						}
						if (!string.IsNullOrEmpty(text2))
						{
							if (textList[i].Text.Length < key.Content.Length - text2.Length)
							{
								text2 += textList[i].Text;
								textList[i].Text = "";
							}
							else
							{
								textList[i].Text = textList[i].Text.Remove(0, key.Content.Length - text2.Length);
								text2 = key.Content;
							}
						}
						if (startIndex >= num3 && startIndex < num3 + textList[i].Text.Length)
						{
							int num4 = startIndex - num3;
							if (textList[i].Text.Length - num4 < key.Content.Length)
							{
								text2 = textList[i].Text.Substring(num4, textList[i].Text.Length - num4);
								textList[i].Text = textList[i].Text.Remove(num4, textList[i].Text.Length - num4) + key.Content;
							}
						}
						num3 += textList[i].Text.Length;
					}
				}
				startIndex++;
			}
			foreach (WordKey item in list)
			{
				string keyValue = GetKeyValue(item);
				foreach (DocumentFormat.OpenXml.Wordprocessing.Text text4 in textList)
				{
					text4.Text = text4.Text.Replace(item.Content, keyValue);
				}
			}
		}

		protected virtual string GetKeyValue(WordKey wordKey)
		{
			if (wordKey.Key == KeyHospital)
			{
				return ConfigSettings.GetInstance().ProjectReportSetting.HospitalName;
			}
			if (wordKey.Key == KeyExperimentName)
			{
				return Experiment.Name;
			}
			if (wordKey.Key == KeyOperator)
			{
				return Experiment.Operator;
			}
			if (wordKey.Key == KeyAssessor)
			{
				return Experiment.Auditor;
			}
			if (wordKey.Key == KeyDate)
			{
				return Experiment.Device.StartTime.ToString("yyyy-MM-dd");
			}
			if (wordKey.Key == KeyStartTime)
			{
				return Experiment.Device.StartTime.ToString("HH:mm:ss");
			}
			if (wordKey.Key == KeyEndTime)
			{
				return Experiment.Device.StopTime.ToString("HH:mm:ss");
			}
			if (wordKey.Key == KeyReagentVer)
			{
				return Experiment.Note;
			}
			if (wordKey.Key == KeyInsNum)
			{
				return Experiment.Device.Series;
			}
			if (Samples == null || Samples.Count == 0 || Samples[0] == null)
			{
				return string.Empty;
			}
			Sample sample = Samples[0];
			if (wordKey.Key == KeyLabel)
			{
				return sample.PatientInfo.ID;
			}
			if (wordKey.Key == KeyName)
			{
				return sample.PatientInfo.Name;
			}
			if (wordKey.Key == KeySex)
			{
				return sample.PatientInfo.Sex;
			}
			if (wordKey.Key == KeyAge)
			{
				return sample.PatientInfo.Age;
			}
			if (wordKey.Key == KeyCaseID)
			{
				return sample.PatientInfo.CaseID;
			}
			if (wordKey.Key == KeyBedID)
			{
				return sample.PatientInfo.BedID;
			}
			if (wordKey.Key == KeyOutPatientID)
			{
				return sample.PatientInfo.OutPatientID;
			}
			if (wordKey.Key == KeyInPatientID)
			{
				return sample.PatientInfo.InPatientID;
			}
			if (wordKey.Key == KeyDetecdtDate)
			{
				return sample.PatientInfo.SpecimenDate;
			}
			if (wordKey.Key == KeyTestDate)
			{
				return Experiment.Device.StartTime.ToShortDateString();
			}
			if (wordKey.Key == KeyReportDate)
			{
				return DateTime.Now.ToShortDateString();
			}
			if (wordKey.Key == KeyOffice)
			{
				return sample.PatientInfo.Office;
			}
			if (wordKey.Key == KeyDiagnosis)
			{
				return sample.PatientInfo.Diagnosis;
			}
			if (wordKey.Key == KeySampleType)
			{
				return sample.PatientInfo.Specimen;
			}
			if (wordKey.Key == KeyDoctor)
			{
				return sample.PatientInfo.Doctor;
			}
			if (wordKey.Key == KeyRemark)
			{
				return sample.PatientInfo.Remark;
			}
			if (string.IsNullOrEmpty(wordKey.PName) && Samples.Count == 1)
			{
				wordKey.PName = Samples[0].Project.BasicInfo.Name;
			}
			sample = Samples.Where((Sample s) => s.Project.BasicInfo.Name == wordKey.PName).FirstOrDefault();
			if (sample == null)
			{
				return string.Empty;
			}
			if (wordKey.Key == KeyProject)
			{
				return sample.Project.BasicInfo.Name;
			}
			if (wordKey.Key == KeyUnit)
			{
				return sample.Project.BasicInfo.Unit;
			}
			Well well = sample.Wells.Where((Well s) => s.MultiTubeID == wordKey.TubeNo).FirstOrDefault();
			SampleTargetItem item = sample.Items.Where((SampleTargetItem s) => s.TubeNo == wordKey.TubeNo && s.ChannelNo == wordKey.ChannelNo).FirstOrDefault();
			BasicOptionItem basicOptionItem = Experiment.CurrentSubset.GetSubsetParamter(sample.Project).BasicOption.Items.Where((BasicOptionItem s) => s.ChannelNo == wordKey.ChannelNo && s.TubeNo == wordKey.TubeNo).FirstOrDefault();
			if (wordKey.Key == KeyTargetName && item != null)
			{
				return item.TargetName;
			}
			if ((wordKey.Key == KeyCn || wordKey.Key == KeyTargetResult) && item != null && basicOptionItem != null)
			{
				return ResultFormat.FormatCnValue(item.Result.CnValue, basicOptionItem.DetectionMin, basicOptionItem.DetectionMax, Experiment.ExperimentSetting.UseScientific ? "0.000E0" : "#,0.00");
			}
			if (wordKey.Key == KeyThreshold && basicOptionItem != null)
			{
				return basicOptionItem.DetectionMin.ToString("0");
			}
			if (wordKey.Key == KeyCt && item != null)
			{
				if (double.IsPositiveInfinity(item.Result.CtValue))
				{
					return Utility.GetMessage("NoCt");
				}
				return item.Result.CtValue.ToString("0.00");
			}
			if (wordKey.Key == KeySampleConclusion)
			{
				return sample.Result.GetAllConclusion();
			}
			if (wordKey.Key == KeyWellConclusion && well != null)
			{
				return well.Result.GetAllConclusion();
			}
			if (wordKey.Key == KeyTargetConclusion && item != null)
			{
				return item.Result.GetAllConclusion();
			}
			if (wordKey.Key == KeyHRMTyping && item != null && item.Result is MeltingTargetResult)
			{
				Subset.SubsetParamter subsetParamter = Experiment.CurrentSubset.GetSubsetParamter(sample.Project);
				if (subsetParamter != null)
				{
					HRMOptionItem hRMOptionItem = subsetParamter.BasicOption.Items.Where((BasicOptionItem s) => s.TargetName == item.TargetName).FirstOrDefault() as HRMOptionItem;
					if (hRMOptionItem != null)
					{
						if (hRMOptionItem.TypeMethod == EHRMTypeMethod.Auto)
						{
							return Experiment.CurrentSubset.GenoTypeList.Where((KeyValuePair s) => (EGenoType)s.Key == ((HRMTargetResult)item.Result).GenoType).FirstOrDefault().Value.ToString();
						}
						return ((HRMTargetResult)item.Result).GenoName;
					}
				}
			}
			if (wordKey.Key == KeyHRMQualityFactor && item != null && item.Result is MeltingTargetResult)
			{
				return ResultFormat.Format(((MeltingTargetResult)item.Result).QualityFactor, "0.00");
			}
			if (wordKey.Key == KeyADTyping && sample.Wells[0].Result is ADWellResult)
			{
				return ((ADWellResult)sample.Wells[0].Result).TypeName;
			}
			if (wordKey.Key == KeyADQualityFactor && sample.Wells[0].Result is ADWellResult)
			{
				return ResultFormat.Format(((ADWellResult)sample.Wells[0].Result).QualityFactor, "0.00");
			}
			for (int i = 0; i < Experiment.GridColumns.Columns.Count; i++)
			{
				ColumnInfo columnInfo = Experiment.GridColumns.Columns[i];
				if (columnInfo.AllowDelete && !string.IsNullOrEmpty(columnInfo.Header) && columnInfo.Header == wordKey.Key)
				{
					if (columnInfo.FieldIndex >= 0 && columnInfo.FieldIndex < sample.PatientInfo.CustomData.Count)
					{
						return sample.PatientInfo.CustomData[columnInfo.FieldIndex];
					}
					return string.Empty;
				}
			}
			if (item == null && well == null)
			{
				MidValue midValue = sample.Result.MidValues.Where((MidValue s) => s.Name == wordKey.Key).FirstOrDefault();
				if (midValue != null)
				{
					return midValue.Value;
				}
				return string.Empty;
			}
			if (item == null && well != null)
			{
				MidValue midValue2 = well.Result.MidValues.Where((MidValue s) => s.Name == wordKey.Key).FirstOrDefault();
				if (midValue2 != null)
				{
					return midValue2.Value;
				}
				return string.Empty;
			}
			MidValue midValue3 = item.Result.MidValues.Where((MidValue s) => s.Name == wordKey.Key).FirstOrDefault();
			if (midValue3 != null)
			{
				return midValue3.Value;
			}
			return string.Empty;
		}

		protected virtual WordKey GetKey(string text)
		{
			string text2 = null;
			if (text.IndexOf(KeyHospital) == 0 || text.IndexOf("$" + Utility.GetMessage("hospital_name")) == 0)
			{
				text2 = KeyHospital;
			}
			if (text.IndexOf(KeyDate) == 0)
			{
				text2 = KeyDate;
			}
			else if (text.IndexOf(KeyExperimentName) == 0)
			{
				text2 = KeyExperimentName;
			}
			else if (text.IndexOf(KeyStartTime) == 0)
			{
				text2 = KeyStartTime;
			}
			else if (text.IndexOf(KeyEndTime) == 0)
			{
				text2 = KeyEndTime;
			}
			else if (text.IndexOf(KeyInsNum) == 0)
			{
				text2 = KeyInsNum;
			}
			else if (text.IndexOf(KeyReagentVer) == 0)
			{
				text2 = KeyReagentVer;
			}
			else if (text.IndexOf(KeyOperator) == 0 || text.IndexOf("$" + Utility.GetMessage("experiment_operator")) == 0)
			{
				text2 = KeyOperator;
			}
			else if (text.IndexOf(KeyAssessor) == 0 || text.IndexOf("$" + Utility.GetMessage("experiment_auditor")) == 0)
			{
				text2 = KeyAssessor;
			}
			else if (text.IndexOf(KeyProject) == 0 || text.IndexOf("$" + Utility.GetMessage("Project")) == 0)
			{
				text2 = KeyProject;
			}
			else if (text.IndexOf(KeyUnit) == 0 || text.IndexOf("$" + Utility.GetMessage("default_unit")) == 0)
			{
				text2 = KeyUnit;
			}
			else if (text.IndexOf(KeyLabel) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_index")) == 0)
			{
				text2 = KeyLabel;
			}
			else if (text.IndexOf(KeyName) == 0 || text.IndexOf("$" + Utility.GetMessage("patient_name")) == 0)
			{
				text2 = KeyName;
			}
			else if (text.IndexOf(KeySex) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_sex")) == 0)
			{
				text2 = KeySex;
			}
			else if (text.IndexOf(KeyAge) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_age")) == 0)
			{
				text2 = KeyAge;
			}
			else if (text.IndexOf(KeyCaseID) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_case_id")) == 0)
			{
				text2 = KeyCaseID;
			}
			else if (text.IndexOf(KeyBedID) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_bed_id")) == 0)
			{
				text2 = KeyBedID;
			}
			else if (text.IndexOf(KeyOutPatientID) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_outpatient_id")) == 0)
			{
				text2 = KeyOutPatientID;
			}
			else if (text.IndexOf(KeyInPatientID) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_inpatient_id")) == 0)
			{
				text2 = KeyInPatientID;
			}
			else if (text.IndexOf(KeyDetecdtDate) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_specimen_date")) == 0)
			{
				text2 = KeyDetecdtDate;
			}
			else if (text.IndexOf(KeyTestDate) == 0 || text.IndexOf("$" + Utility.GetMessage("start_time")) == 0)
			{
				text2 = KeyTestDate;
			}
			else if (text.IndexOf(KeyReportDate) == 0)
			{
				text2 = KeyReportDate;
			}
			else if (text.IndexOf(KeyOffice) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_office")) == 0)
			{
				text2 = KeyOffice;
			}
			else if (text.IndexOf(KeyDiagnosis) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_diagnosis")) == 0)
			{
				text2 = KeyDiagnosis;
			}
			else if (text.IndexOf(KeySampleType) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_specimen")) == 0)
			{
				text2 = KeySampleType;
			}
			else if (text.IndexOf(KeyDoctor) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_doctor")) == 0)
			{
				text2 = KeyDoctor;
			}
			else if (text.IndexOf(KeyRemark) == 0 || text.IndexOf("$" + Utility.GetMessage("sample_remark")) == 0)
			{
				text2 = KeyRemark;
			}
			else if (text.IndexOf(KeyTargetName) == 0 || text.IndexOf("$" + Utility.GetMessage("Target")) == 0)
			{
				text2 = KeyTargetName;
			}
			else if (text.IndexOf(KeyTargetResult) == 0 || text.IndexOf("$" + Utility.GetMessage("Cn")) == 0)
			{
				text2 = KeyTargetResult;
			}
			else if (text.IndexOf(KeyThreshold) == 0 || text.IndexOf("$" + Utility.GetMessage("Prj_LowLimit")) == 0)
			{
				text2 = KeyThreshold;
			}
			else if (text.IndexOf(KeyCt) == 0 || text.IndexOf("$" + Utility.GetMessage("Ct")) == 0)
			{
				text2 = KeyCt;
			}
			else if (text.IndexOf(KeyCn) == 0 || text.IndexOf("$" + Utility.GetMessage("Cn")) == 0)
			{
				text2 = KeyCn;
			}
			else if (text.IndexOf(KeyTargetConclusion) == 0 || text.IndexOf("$" + Utility.GetMessage("Prj_Conclusion")) == 0)
			{
				text2 = KeyTargetConclusion;
			}
			else if (text.IndexOf(KeyWellConclusion) == 0 || text.IndexOf("$" + Utility.GetMessage("Prj_WellConclusion")) == 0)
			{
				text2 = KeyWellConclusion;
			}
			else if (text.IndexOf(KeySampleConclusion) == 0 || text.IndexOf("$" + Utility.GetMessage("Prj_SampleConclusion")) == 0)
			{
				text2 = KeySampleConclusion;
			}
			else if (text.IndexOf(KeyHRMTyping) == 0 || (text.IndexOf("$" + Utility.GetMessage("typing")) == 0 && Experiment.Type == EProjectType.HRM))
			{
				text2 = KeyHRMTyping;
			}
			else if (text.IndexOf(KeyHRMQualityFactor) == 0 || (text.IndexOf("$" + Utility.GetMessage("quality_factor")) == 0 && Experiment.Type == EProjectType.HRM))
			{
				text2 = KeyHRMQualityFactor;
			}
			else if (text.IndexOf(KeyADTyping) == 0 || (text.IndexOf("$" + Utility.GetMessage("typing")) == 0 && Experiment.Type == EProjectType.AD))
			{
				text2 = KeyADTyping;
			}
			else if (text.IndexOf(KeyADQualityFactor) == 0 || (text.IndexOf("$" + Utility.GetMessage("quality_factor")) == 0 && Experiment.Type == EProjectType.AD))
			{
				text2 = KeyADQualityFactor;
			}
			else if (text.IndexOf(KeyCurve) == 0 || text.IndexOf("$" + Utility.GetMessage("amplification_program")) == 0)
			{
				text2 = KeyCurve;
			}
			else
			{
				foreach (ColumnInfo column in Experiment.GridColumns.Columns)
				{
					if (column.AllowDelete && !string.IsNullOrEmpty(column.Header) && (text.IndexOf("$" + column.Header + "#") == 0 || text.IndexOf("$" + column.Header + '|') == 0))
					{
						text2 = column.Header;
						break;
					}
				}
			}
			string[] texts = text.Split('|');
			if (string.IsNullOrEmpty(text2))
			{
				text2 = texts[0].Substring(1, texts[0].Length - ((texts.Count() != 1) ? 1 : 2));
			}
			if (string.IsNullOrEmpty(text2))
			{
				return null;
			}
			WordKey wordKey = new WordKey();
			wordKey.Key = text2;
			wordKey.Content = text;
			int num = 2;
			int result = -1;
			int result2 = -1;
			if (texts.Count() > 1)
			{
				if (string.IsNullOrEmpty(texts[1]))
				{
					return wordKey;
				}
				string text3 = (from s in Samples
					where (texts.Count() > 2 && texts[1] == s.Project.BasicInfo.Name) || (texts.Count() == 2 && texts[1].Substring(0, texts[1].Count() - 1) == s.Project.BasicInfo.Name)
					select s.Project.BasicInfo.Name).FirstOrDefault();
				if (string.IsNullOrEmpty(text3))
				{
					if (!int.TryParse(texts[1].Substring(0, 1), out result))
					{
						wordKey.PName = texts[1];
						return wordKey;
					}
					num = 1;
				}
				else
				{
					wordKey.PName = text3;
					if (text3 != texts[1])
					{
						return wordKey;
					}
				}
			}
			if (texts.Count() > num)
			{
				if (string.IsNullOrEmpty(texts[num]))
				{
					return wordKey;
				}
				if (texts[num].Count() == 1)
				{
					if (!int.TryParse(texts[num], out result))
					{
						return wordKey;
					}
					wordKey.TubeNo = result;
				}
				else
				{
					if (!int.TryParse(texts[num].Substring(0, 2), out result) && !int.TryParse(texts[num].Substring(0, 1), out result))
					{
						return wordKey;
					}
					wordKey.TubeNo = result;
					if (texts[num].Count() != result.ToString().Count())
					{
						return wordKey;
					}
				}
			}
			if (texts.Count() > num + 1)
			{
				if (string.IsNullOrEmpty(texts[num + 1]))
				{
					return wordKey;
				}
				if (!int.TryParse(texts[num + 1].Substring(0, 1), out result2))
				{
					return wordKey;
				}
				wordKey.ChannelNo = result2;
				if (texts[num + 1].Count() != result2.ToString().Count())
				{
					return wordKey;
				}
			}
			return wordKey;
		}
	}
}
